#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:set sts=4:tabstop=4:expandtab:shiftwidth=4:
# EasyUbuntu - Install commonly requested apps on your (k/X)ubuntu box 
# Copyright (C) 2006 EasyUbuntu team
# https://launchpad.net/products/easyubuntu
# http://easyubuntu.freecontrib.org
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
# 
import sys
from qt import *
from qtloggerauto import EuQtLogger
from backend import *

class QtLogger(EuQtLogger):
    def __init__(self, preconfigslist, packageslist, configslist, confdir):
        EuQtLogger.__init__(self)
        self.commandslist = []
        self.preconfigslist = preconfigslist
        self.packageslist = packageslist
        self.configslist = configslist

        self.setSizeGripEnabled(1)
        
        self.textBrowser.clear()
        self.textBrowser.setOverwriteMode(False)

        #print "Entered Process Function"
        self.process = QProcess()
        self.process.clearArguments()
        
        output = processPreconfig(preconfigslist)

        self.pkgcommandslist = processInstall(packageslist,  confdir)
        self.cfgcommandslist = processConfig(configslist, confdir)
       
        for cmds in self.pkgcommandslist:
            self.commandslist.append(cmds)
        for cmds in self.cfgcommandslist:
            self.commandslist.append(cmds)
        for commands in self.commandslist:
            self.textBrowser.append(QString(commands))

        self.runCommand()
    
    def runCommand(self):
        if self.commandslist:
            self.process.clearArguments()
            self.process.setArguments(QStringList.split(" ",self.commandslist[0]))
            self.connect(self.process, SIGNAL("readyReadStdout()"), self.readOutput)
            self.connect(self.process, SIGNAL("readyReadStderr()"), self.readErrors)
            self.connect(self.process, SIGNAL("processExited()"), self.resetButtons)
            self.buttonCloselog.setEnabled(False)
            if self.process.start():
                self.textBrowser.append(QString("Executing: "))
                self.textBrowser.append(QString(self.commandslist[0]))
        else:
            return

    def readOutput(self):
        #print "PID in readoutput", self.process.processIdentifier()
        self.textBrowser.append(QString(self.process.readStdout()))

    def readErrors(self):
        #print "PID in readerrors", self.process.processIdentifier()
        self.textBrowser.append(QString("error: "))
        self.textBrowser.append(QString(self.process.readLineStderr()))

    def resetButtons(self):
        #print "PID in resetbuttons", self.process.processIdentifier()
        self.buttonCloselog.setEnabled(True)
        if self.commandslist:
            self.commandslist.pop(0)
        else:
            self.textBrowser.append(QString("EasyUbuntu is finished. You may copy this log for debugging purposes."))

        self.runCommand()
